package cn.edu.scau.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.scau.entity.Download;
import cn.edu.scau.service.DownloadService;

@Controller
@RequestMapping("/download")
public class DownloadTest {

	@Autowired
	private DownloadService downloadService;

	public DownloadService getDownloadService() {
		return downloadService;
	}

	public void setDownloadService(DownloadService downloadService) {
		this.downloadService = downloadService;
	}

	@RequestMapping(value = "/save")
	public String save(HttpServletRequest request) {

		Download download = new Download();
		download.setColumnId(20);
		download.setEditor("mingweicai");
		download.setLikeTimes(20);
		download.setReleaseDate("2014-03-19");
		download.setResourceUrl("http://www.baidu.com");
		download.setSource("校科联");
		download.setTitle("中国惊奇先生");
		download.setStatus(true);
		this.getDownloadService().add(download);
		return "NewFile";
	};

	@RequestMapping("/delete")
	public String delete() {

		Download download = (Download) this.getDownloadService().getDownloadByID(1);
		System.out.println(download);
		this.getDownloadService().delete(6);
		return "NewFile";
	};

	@RequestMapping("/update")
	public String update() {

		Download download = (Download) this.getDownloadService().getDownloadByID(2);
		download.setResourceUrl("http://www.baidu..com");
		this.getDownloadService().update(download, download.getDownload_id());
		return "NewFile";
	};

	@ResponseBody
	@RequestMapping("/front")
	public List findByPageFront() {
		System.out.println(this.getDownloadService().countTheEntityNum(20, 10, true));
		return this.getDownloadService().getDownloadsProperty(4, 1, 20, true);
	};

	@ResponseBody
	@RequestMapping("/management")
	public List findByPageManagement() {
		System.out.println(this.getDownloadService().countTheEntityNum(10, 10, false));
		return this.getDownloadService().getDownloadsProperty(4, 1, 10, false);
	};

	@ResponseBody
	@RequestMapping("/rank")
	public List rank() {

		return this.getDownloadService().getDownloadsByRank(20);
	}

}
